package com.testdocs.client.signin;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.testdocs.client.ClientFactory;
import com.testdocs.client.project.ProjectInfo;
import com.testdocs.client.project.ProjectService;
import com.testdocs.client.project.ProjectServiceAsync;
import com.testdocs.client.project.event.ProjectsListLoadedEvent;
import com.testdocs.client.project.i18n.ProjectConstants;
import com.testdocs.client.signin.event.SignInEvent;
import com.testdocs.client.signin.event.SignOutEvent;
import com.testdocs.client.signin.i18n.SignInConstants;

public class CheckSignInActivity implements Activity {
	private ClientFactory clientFactory;
	private Activity activity;

	public CheckSignInActivity(ClientFactory clientFactory, Activity activity) {
		this.clientFactory = clientFactory;
		this.activity = activity;
	}

	@Override
	public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
		SignInConstants signInConstants = (SignInConstants) GWT.create(SignInConstants.class);
		
		clientFactory.getWaitingPopupPanel().show(signInConstants.checkingSignInStatus());
		SignInServiceAsync signInService = GWT.create(SignInService.class);
		signInService.signIn(Window.Location.getHref(), GWT.getHostPageBaseURL(), new AsyncCallback<UserInfo>() {
			public void onFailure(Throwable error) {
				clientFactory.getWaitingPopupPanel().hide();
				clientFactory.getRpcErrorHandler().handle(error);
			}

			public void onSuccess(UserInfo result) {
				if (result.isSignedIn()) {
					clientFactory.getEventBus().fireEvent(new SignInEvent(result));
					clientFactory.getWaitingPopupPanel().hide();
					loadUserProjects(panel, eventBus);
				} else {
					clientFactory.getEventBus().fireEvent(new SignOutEvent());
					clientFactory.getWaitingPopupPanel().hide();
					clientFactory.getPlaceController().goTo(new SignInPlace(result));
				}
			}
		});
	}

	@Override
	public String mayStop() {
		return activity.mayStop();
	}

	@Override
	public void onCancel() {
		activity.onCancel();
	}

	@Override
	public void onStop() {
		activity.onStop();
	}
	
	private void loadUserProjects(final AcceptsOneWidget panel, final EventBus eventBus) {
		ProjectConstants projectConstants = (ProjectConstants) GWT.create(ProjectConstants.class);
		
		clientFactory.getWaitingPopupPanel().show(projectConstants.loadingProjectsInfoStatus());
		ProjectServiceAsync service = GWT.create(ProjectService.class);
		service.getProjects(new AsyncCallback<ProjectInfo[]>() {
			@Override
			public void onFailure(Throwable caught) {
				clientFactory.getRpcErrorHandler().handle(caught);
				clientFactory.getWaitingPopupPanel().hide();
			}

			@Override
			public void onSuccess(ProjectInfo[] result) {
				clientFactory.getEventBus().fireEvent(new ProjectsListLoadedEvent(result));
				clientFactory.getWaitingPopupPanel().hide();
				// won't work, because place is the same
				// clientFactory.getPlaceController().goTo(place);
				// starting activity manually:
				activity.start(panel, eventBus);
			}
		});
	}
	
}
